<template>
  <div>
    <uploadoss ref="refuploadoss" />
    <el-button type="primary" @click="uploadbtn">确定</el-button>

    <uploadFile2 ref="uploadFile2"></uploadFile2>
  </div>
</template>

<script>
  import uploadoss from '@/components/Upload/uploadimage.vue'
  import uploadFile2 from '@/components/Upload/uploadFile2.vue'
  //import moment from 'moment'
  import {client,headers_img} from '@/api/oss'
  export default {
    name: 'test3',
    components: {
      uploadoss,uploadFile2
    },
    methods: {
      uploadbtn () {
        if (this.$refs.refuploadoss.files.length !== 0) {
          this.ossput(this.$refs.refuploadoss.files)
        } else {
          this.$message.error('请选择图片')
        }
      },
      async ossput (file) {
        // 获取文件后缀名，如'.jpg'
        let index = file.name.lastIndexOf('.')
        let type = file.name.substr(index)

        // 随机生成16进制
        let fileName = Math.random().toString(16).slice(2)
        // 拼接新文件名（随机数+文件后缀名）
        let fileNames = `${fileName}${type}`

       // let month = moment(Date.now()).format('YYYY-MM')

        // 上传文件,这里是上传到OSS的 upload文件夹下
        client.put('upload/' + fileNames, file,headers_img).then(res => {
          if (res.res.statusCode === 200) {
          //  console.error("https://files.gamepro.vip/upload/"+fileNames);
            this.$message.success('上传成功！')
          } else {
            this.$message.error('上传失败！')
          }
        })
      }

    }
  }
</script>
